﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Plumbing.ParamForm
{
	public partial class FloorHeightForm : System.Windows.Forms.Form
	{
		public FloorHeightForm(OptionData option, InBuildSystem buildSystem)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.option = option;
			this.buildSystem = buildSystem;
		}

		private void FloorHeightForm_Load(object sender, EventArgs e)
		{
			this.LoadData();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.hyTxtFloorNum.Text == "")
			{
				YJKMessageBox.Information("参数不能为空。");
				return;
			}
			if (double.Parse(this.hyTxtFloorNum.Text) == 0.0)
			{
				YJKMessageBox.Information("楼层数不能为0。");
				return;
			}
			this.SaveData();
			this.SetFloor();
			base.DialogResult = DialogResult.OK;
		}

		private void LoadData()
		{
			this.hyTxtFloorNum.Text = this.option.FloorNum.ToString();
			this.listhHyFloors = this.option.FloorList;
			this.dgvFloor.Rows.Clear();
			foreach (HYFloor hyfloor in this.listhHyFloors)
			{
				this.dgvFloor.Rows.Add(new object[]
				{
					"",
					hyfloor.Height
				});
			}
			this.hyTxtFloorNum.TextChanged += this.hyTxtFloorNum_TextChanged;
		}

		private void SaveData()
		{
			this.option.FloorNum = int.Parse(this.hyTxtFloorNum.Text);
			this.listhHyFloors.Clear();
			for (int i = 0; i < this.dgvFloor.Rows.Count; i++)
			{
				double height = 0.0;
				if (this.dgvFloor.Rows[i].Cells[1].Value.ToString() != "")
				{
					height = double.Parse(this.dgvFloor.Rows[i].Cells[1].Value.ToString());
				}
				HYFloor item = new HYFloor(i + 1, height);
				this.listhHyFloors.Add(item);
			}
			this.option.FloorList = this.listhHyFloors;
		}

		private void SetFloor()
		{
			foreach (ShareRPipeSystem shareRPipeSystem in this.buildSystem.SrpSystems)
			{
				int count = this.option.FloorList.Count;
				int num = count - shareRPipeSystem.FloorSystems.Count;
				if (num > 0)
				{
					for (int i = 0; i < num; i++)
					{
						shareRPipeSystem.FloorSystems.Add(new FloorSystem(shareRPipeSystem.FloorSystems[0]));
						shareRPipeSystem.SupplyPipes.Add(new HYPipeData(shareRPipeSystem.SupplyPipes[0]));
						shareRPipeSystem.ReturnPipes.Add(new HYPipeData(shareRPipeSystem.ReturnPipes[0]));
					}
				}
				else
				{
					for (int j = 0; j < -num; j++)
					{
						shareRPipeSystem.FloorSystems.RemoveAt(count);
						shareRPipeSystem.SupplyPipes.RemoveAt(count);
						shareRPipeSystem.ReturnPipes.RemoveAt(count);
					}
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnModify_Click(object sender, EventArgs e)
		{
			if (this.hyTxtHeight.Text == "" || (this.hyTxtHeight.Text != "" && double.Parse(this.hyTxtHeight.Text) > 100.0))
			{
				YJKMessageBox.Information("请输入0~100之间的数字。");
				this.hyTxtHeight.Focus();
				return;
			}
			for (int i = 0; i < this.dgvFloor.SelectedRows.Count; i++)
			{
				int index = this.dgvFloor.SelectedRows[i].Index;
				this.dgvFloor.Rows[index].Cells[1].Value = double.Parse(this.hyTxtHeight.Text).ToString("0.00");
			}
		}

		private void hyTxtFloorNum_TextChanged(object sender, EventArgs e)
		{
			IList<HYFloor> list = new List<HYFloor>();
			int num = int.Parse(this.hyTxtFloorNum.Text);
			if (num > 200)
			{
				num = 7;
				this.hyTxtFloorNum.Text = num.ToString();
			}
			int count = this.dgvFloor.Rows.Count;
			for (int i = 0; i < count; i++)
			{
				double height = 0.0;
				if (this.dgvFloor.Rows[i].Cells[1].Value.ToString() != "")
				{
					height = double.Parse(this.dgvFloor.Rows[i].Cells[1].Value.ToString());
				}
				HYFloor item = new HYFloor(i + 1, height);
				list.Add(item);
			}
			this.dgvFloor.Rows.Clear();
			if (num <= count)
			{
				for (int j = 0; j < num; j++)
				{
					this.dgvFloor.Rows.Add(new object[]
					{
						"",
						list[j].Height
					});
				}
				return;
			}
			for (int k = 0; k < count; k++)
			{
				this.dgvFloor.Rows.Add(new object[]
				{
					"",
					list[k].Height
				});
			}
			for (int l = 0; l < num - count; l++)
			{
				if (this.hyTxtHeight.Text != "")
				{
					this.dgvFloor.Rows.Add(new object[]
					{
						"",
						double.Parse(this.hyTxtHeight.Text).ToString("0.00")
					});
				}
				else
				{
					this.dgvFloor.Rows.Add(new object[]
					{
						"",
						"3.00"
					});
				}
			}
		}

		private void dgvFloor_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
		{
			int num = e.RowIndex + 1;
			SolidBrush brush = new SolidBrush(this.dgvFloor.RowHeadersDefaultCellStyle.ForeColor);
			e.Graphics.DrawString(num.ToString(CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, brush, (float)(e.RowBounds.Location.X + 5), (float)(e.RowBounds.Location.Y + 4));
		}

		private void dgvFloor_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dgvFloor.CurrentCell.ColumnIndex == 1)
			{
				e.Control.KeyPress += this.dataGridViewCell_KeyPress;
			}
			this.hyTxtHeight.Text = this.dgvFloor.CurrentCell.Value.ToString();
		}

		private void dataGridViewCell_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}

		private void dgvFloor_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			if (this.beCheck)
			{
				e.Cancel = false;
				return;
			}
			if (this.dgvFloor.Columns[e.ColumnIndex].Name == "colHeight" || e.RowIndex > 0)
			{
				try
				{
					double num = Convert.ToDouble(e.FormattedValue.ToString());
					if (num <= 0.0 || num > 20.0)
					{
						YJKMessageBox.Show("楼层高度应在0-20之间，不包含0！");
						e.Cancel = true;
					}
				}
				catch (Exception)
				{
					YJKMessageBox.Show("楼层高度值不能为空！");
					e.Cancel = true;
				}
			}
		}

		private void btnCancel_MouseLeave(object sender, EventArgs e)
		{
			this.beCheck = false;
		}

		private void btnCancel_MouseEnter(object sender, EventArgs e)
		{
			this.beCheck = true;
		}

		private OptionData option;

		private InBuildSystem buildSystem;

		private IList<HYFloor> listhHyFloors = new List<HYFloor>();

		private bool beCheck;
	}
}
